/**
 * string 转arrayBuffer
 * @param {*} str 
 * @returns 
 */
export function strToab(str) {
    let buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
    let bufView = new Uint8Array(buf)
    for (let i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i)
    }
    return buf
}

/**
 * arrayBuffer 转string
 * @param {*} buf 
 * @returns 
 */
export function abTostr(buf) {
    return String.fromCharCode.apply(null, new Uint8Array(buf));
}
/**
 * 
 * @returns 字符串转buffer
 */
export function strTobf(){
    return Buffer.from(str)
}
/**
 * buffer 转字符串
 * @param {*} buf 
 * @param {*} format 
 * @returns 
 */
export function bfTostr(buf, format ='utf-8'){
    return buf.toString(format)
}